<?php
namespace Admin\Controller;
use Admin\Controller\AiController;
class PushController extends AiController {
	//推行到百度（实时）
	public function BaiDuIndex(){
		$this->display();
	}
    //百度推送操作
	public function BaiduPost(){
	  ini_set("max_execution_time",18000);
	  $dbname=I('push_dbname');
      $status=I('status');//0未修改 1已修改 3全部
      $num=I('num');
      $tuistatus=I('tuistatus');//推送筛选 0筛选出未被推送的 1不筛选
      switch ($status) {
      	case '0':
      	$where_status['bai_status']=0;
      	break;
      	default :
      	$where_status['bai_status']=1;
      	break;
      }
      $TableFiles='pinyin';
      $addid=get_add_id($dbname);//自增长字段名字
      $select_info=D($dbname)->where($where_status)->limit($num)->select();
      $id_sting=$this->arrayInfoGetInId($select_info,$addid);//获取id字符串
      $array_info=$this->toDbNameGetUrl($dbname,$select_info,$addid,$tuistatus);//获取url数组
      $statusInfo=$this->toIdPushToBai($array_info);
      if(isset($statusInfo['error'])){
      	$this->error($statusInfo['message']); 
      	return;
      }
      $where['id'] =array('in', $id_sting);
      $save['bai_status']=1;
      $table_save_return=se($dbname,$where,$save);
      $select_info_not=D($dbname)->where('bai_status=0')->count();
      $this->success('还可以提交'.$statusInfo['remain'].'个链接 ，还剩'.$select_info_not.'没有提交','',10);
  }


//获取数组中自增长值 并且拼接字符串
  public function arrayInfoGetInId($select_info,$addid){
  	foreach ($select_info as $key => $value) {
  		$id_string.=$value[$addid].',';
  	}
  	return rtrim($id_string,',');
  }

//通过不同的参数名字 获取不同到的URL
  public function toDbNameGetUrl($dbname,$select_info,$addid,$tuistatus=0){
  	$url='';
  	$TableFiles='';
  	$html='.html';
  	$url_array=array();
  	switch ($dbname) {
  		case 'Chengyu':
  		$url='';
  		$TableFiles='pinyin';
  		break;

  		case 'ChengyuCaitu':
  		$url='ktccy/';
  		$TableFiles=$addid;
  		break;

  		case 'ChengyuCount':
  		$url='zi/';
  		$TableFiles='unicode';
  		break;
  		case 'ChengyuDuilian':
  		$url='duiduizi/';
  		$TableFiles='pinyin';
  		break;
  		case 'ChengyuGushi':
  		$url='gushi/';
  		$TableFiles='pinyin';
  		break;
  		case 'ChengyuMiyu':
  		$url='miyu/';
  		$TableFiles=$addid;
  		break;	
  		default:
  		$url='cydq/';
  		$TableFiles='pinyin';
  		$html='/';
  		break;					
  	}
  	foreach ($select_info as $key => $value) {
  		$url='';
  		$url=C('BAIDU_PUSH').$url.$value[$TableFiles].$html;
  		//不做url收录筛选
  		if($tuistatus){
  			$url_array[]=$url;
  		//做url收录筛选	
  		}else{
  			$checkBaidu=$this->checkBaidu($url);
  			if($checkBaidu==-1){
  				$url_array[]=$url;
  			}
  		}
  	}
  	return $url_array;
  }
    //通过id 获取信息到百度推送
  public function toIdPushToBai($urls){
  	$api = 'http://data.zz.baidu.com/urls?site='.C('BAIDU_PUSH').'&token='.C(BAIDUTOKEN);
  	$ch = curl_init();
  	$options =  array(
  		CURLOPT_URL => $api,
  		CURLOPT_POST => true,
  		CURLOPT_RETURNTRANSFER => true,
  		CURLOPT_POSTFIELDS => implode("\n", $urls),
  		CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
  	);
  	curl_setopt_array($ch, $options);
  	$result = json_decode(curl_exec($ch),true);
  	return $result;
  }
    //百度推送成功之后 需要修改状态
  public function saveTableStatus($dbname,$string,$addid){
  	$where[$addid]=$arrayValue[$addid];
  	$save['bai_status']=1;
  	return se($dbname,$where,$save);
  }

  function checkBaidu($url) { 
  	$url = 'http://www.baidu.com/s?wd=' . $url; 
  	$curl = curl_init(); 
  	curl_setopt($curl, CURLOPT_URL, $url); 
  	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  	$rs = curl_exec($curl); 
  	curl_close($curl); 
if (!strpos($rs, '没有找到')) { //没有找到说明已被百度收录 
	return 1; 
} else { 
	return -1; 
} 
}
}